package br.com.trophy.trophies;

import java.util.Vector;

import br.com.trophy.parsing.Game;
import br.com.trophy.parsing.ParsingTrophies;
import br.com.trophy.parsing.Trophy;
import android.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyTrophies extends ListActivity
{
	// --------------------------------------------------------------------------------
	// Atributos de Classe
	// --------------------------------------------------------------------------------
	private String[] values;
	private ArrayAdapter<String> adapter;
	private Intent i;
	private String user;
	private Game game;
	private String id;
	private Vector<Trophy> trofeus;
	private int indice;

	// --------------------------------------------------------------------------------
	// Metodos Sobrecarregados
	// --------------------------------------------------------------------------------
	@Override
	public void onCreate( Bundle bundle )
	{
		super.onCreate( bundle );
		i = new Intent( this, TrophyView.class );
		getExtras();
		setAdapter();
	}
	// --------------------------------------------------------------------------------
	@Override
	protected void onListItemClick( ListView l, View v, int position, long id )
	{
		super.onListItemClick( l, v, position, id );
		// bean = manager.selectTrophiesById( position );
		indice = position;
		putExtras();
		startActivity( i );
	}
	// --------------------------------------------------------------------------------
	// Metodos Publicos
	// --------------------------------------------------------------------------------
	public void setAdapter()
	{
		user = "arleudo";
		trofeus = ParsingTrophies.getTrophies( user, id );
		values = new String[trofeus.size()];

		if ( trofeus.size() == 0 )
		{
			Toast.makeText( this,
					"Você não possui nenhum troféu para este jogo!",
					Toast.LENGTH_SHORT ).show();
			finish();
		} else
		{

			for ( int i = 0; i < values.length; i++ )
			{
				values[i] = trofeus.get( i ).getName();
			}

			adapter = new ArrayAdapter<String>( this,
					R.layout.simple_list_item_1, values );
			setListAdapter( adapter );
		}
	}
	// --------------------------------------------------------------------------------
	// Metodos Privados
	// --------------------------------------------------------------------------------
	private void getExtras()
	{
		Bundle extras = getIntent().getExtras();
		game = (Game) extras.getParcelable( "game" );
		id = game.getId();
	}
	// --------------------------------------------------------------------------------
	private void putExtras()
	{
		i.putExtra( "trophies", trofeus.get( indice ) );
		i.putExtra( "game", game );
	}
}
